/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.inventory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.inventory.ItemBonus;
import utility.NameValueList;
import utility.StringUtils;
import utility.typeSafe.EnumUtils;
import utility.typeSafe.HitungBonus;
import utility.typeSafe.JenisBonus;
import utility.typeSafe.SatuanBonus;

/**
 *
 * @author jay
 */
public class ItemBonusTM extends AbstractTableModel {
    
    private List<NameValueList> hitungList = EnumUtils.enumToNameValueList(HitungBonus.class);
    private List<NameValueList> satuanList = EnumUtils.enumToNameValueList(SatuanBonus.class);
    private List<NameValueList> jenisList = EnumUtils.enumToNameValueList(JenisBonus.class);
    private List<ItemBonus> data;

    public ItemBonusTM(List<ItemBonus> data) {
        this.data = data;
    }

    public ItemBonusTM() {
        this(new ArrayList<ItemBonus>(0));
    }

    public ItemBonus get(int index) {
        return data.get(index);
    }

    public void add(ItemBonus product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, ItemBonus product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<ItemBonus> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }
    
    @Override
    public boolean isCellEditable(int row, int column) {
        return true;
    }
    
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 8;
    }

    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        int angka;
        switch (columnIndex) {

            case 3: //batas
                angka = StringUtils.textToInt((String)value);
                data.get(rowIndex).setBatas(angka);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;
            case 4: //batas
                angka = StringUtils.textToInt((String)value);
                data.get(rowIndex).setBonus(angka);
                fireTableCellUpdated(rowIndex, columnIndex);
                break; 
                
            case 5: //START DATE
                Date startDate = StringUtils.stringEditorToDate(value);
                data.get(rowIndex).setBeginDate(startDate);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;       
                
            case 6: //END DATE
                Date endDate = StringUtils.stringEditorToDate(value);
                data.get(rowIndex).setEndDate(endDate);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;           
        }
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        //display data
        switch (columnIndex) {
            case 0: 
                int idx =  NameValueList.indexOf(hitungList, data.get(rowIndex).getCaraHitung());
                if (idx > -1) { 
                    NameValueList jenis = hitungList.get(idx);
                    return jenis.getValue();
                } return null;
                
            case 1: 
                int idx2 =  NameValueList.indexOf(jenisList, data.get(rowIndex).getJenisBonus());
                if (idx2 > -1) { 
                    NameValueList jenis = jenisList.get(idx2);
                    return jenis.getValue();
                } return null;
                
            case 2: 
                int idx3 =  NameValueList.indexOf(satuanList, data.get(rowIndex).getSatuanBonus());
                if (idx3 > -1) { 
                    NameValueList jenis = satuanList.get(idx3);
                    return jenis.getValue();
                } return null;
                
            case 3: return data.get(rowIndex).getBatas();
            case 4: return data.get(rowIndex).getBonus();

            case 5: 
                if (data.get(rowIndex).getBeginDate()!=null){
                    return StringUtils.formatDate(data.get(rowIndex).getBeginDate());
                } return null;
                
            case 6: 
                if (data.get(rowIndex).getEndDate()!=null){
                    return StringUtils.formatDate(data.get(rowIndex).getEndDate());
                } return null;
                
            case 7: 
                if (data.get(rowIndex).getJenisBonus().equals(JenisBonus.TT.toString())) {
                    if (data.get(rowIndex).getHadiah() != null) {
                        return data.get(rowIndex).getHadiah().getNama();
                    } 
                } return null;

            default: return null;
        }
    }
     
//    @Override
//    public Class<?> getColumnClass(int columnIndex) {
//        switch(columnIndex){
//            case 6: return Date.class;
//            case 7: return Date.class;
//        default: return Object.class;
//        }
//    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "HITUNG";
            case 1: return "JENIS";
            case 2: return "SATUAN";
            case 3: return "BATAS";
            case 4: return "JUMLAH";
            case 5: return "START DATE";
            case 6: return "END DATE";
            case 7: return "ITEM BONUS";
            default: return null;
        }
    }
    
}
